package com.dididoctor.patient.Activity.Usercentre.Record.Cases;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetailUp.CasesDetailUpActivity;
import com.dididoctor.patient.Activity.Usercentre.Record.RecordActivity;
import com.dididoctor.patient.MV.BaseFragment;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Ui.customlistview.OnRefreshListener;
import com.dididoctor.patient.Ui.customlistview.RefreshListView;
import com.dididoctor.patient.Utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/15.
 */
public class CasesFragment extends BaseFragment implements CasesView, View.OnClickListener {

    private Activity activity;
    private RefreshListView lv_forum;
    private CasesAdapter adapter;
    private List<CasesBean> casesBeans = new ArrayList<>();
    private int pageIndex = 1;
    private CasesPresenter presenter;

    private Button btn_confirm;

    @Override
    public void onAttach(Activity activity) {
        this.activity = activity;
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_cases, null);
        this.initView(rootView);
        return rootView;

    }

    private void initView(View rootView) {
        lv_forum = (RefreshListView) rootView.findViewById(R.id.lv_forum);
        adapter = new CasesAdapter(activity, casesBeans);
        lv_forum.setAdapter(adapter);
        lv_forum.setHeadAndFoot(true,true);
        lv_forum.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                presenter.getCases(pageIndex);
            }

            @Override
            public void onLoadMoring() {
                pageIndex ++;
                presenter.getCases(pageIndex);
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        btn_confirm = (Button) rootView.findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(this);
        presenter = new CasesPresenter(activity, this);
        presenter.getCases(pageIndex);
    }

    @Override
    public void getCasesSucced(List<CasesBean> casesBeans) {

        if (!Util.isEmpty(casesBeans)) { //如果获得的数据不为空
            lv_forum.setVisibility(View.VISIBLE);
            if (pageIndex == 1) {
                adapter.setData(casesBeans);
                lv_forum.onRefreshFinish();
            }else{
                adapter.addData(casesBeans);
                if(casesBeans.size()< ConstantValue.LOAD_MESSAGE_COUNT){
                    lv_forum.showNoMoreData();
                }else{
                    lv_forum.hideNoMoreData();
                }
            }
        } else {//为空
            if (pageIndex == 1) {
                lv_forum.setVisibility(View.GONE);
            }else{
                lv_forum.showNoMoreData();
            }
        }
//        if (casesBeans != null && casesBeans.size() > 0) {
//            if (pageIndex == 1) {
//                this.casesBeans.clear();
//            }
//            this.casesBeans.addAll(casesBeans);
//            adapter.notifyDataSetChanged();
//        }
//        lv_forum.onRefreshFinish();
    }

    @Override
    public void getCasesFail() {
        lv_forum.onRefreshFinish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                Intent intent = new Intent(activity, CasesDetailUpActivity.class);
                ((RecordActivity) activity).startActivityForResult(intent, 1003);
                break;
        }
    }

    public void getcases() {
        presenter.getCases(pageIndex);
    }

}
